package com.fzy.common.util;

import javax.servlet.ServletRequest;

/**
 * 请求封装工具类
 *
 * @author 冯赵杨
 * @date 2018/7/17 下午3:08
 */
public class RequestUtil {

    private RequestUtil() {
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static String getParam(ServletRequest request, String name) {
        return getParam(request, name, "");
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static String getParam(ServletRequest request, String name, String defaultStr) {
        String temp = request.getParameter(name);
        if (temp == null || "".equals(temp.trim())) {
            return defaultStr;
        }
        return temp.trim();
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static Integer getIntParam(ServletRequest request, String name) {
        return getIntParam(request, name, null);
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static Integer getIntParam(ServletRequest request, String name, Integer defaultNum) {
        String temp = request.getParameter(name);
        if (temp == null || "".equals(temp.trim())) {
            return defaultNum;
        }
        return Integer.valueOf(temp);
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static Boolean getBooleanParam(ServletRequest request, String name) {
        return getBooleanParam(request, name, false);
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static Boolean getBooleanParam(ServletRequest request, String name, boolean defaultVal) {
        String temp = request.getParameter(name);
        if (temp == null || "".equals(temp.trim())) {
            return defaultVal;
        }
        return Boolean.valueOf(temp);
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static Integer[] getIntParams(ServletRequest request, String name) {
        String[] paramValues = request.getParameterValues(name);
        if (paramValues == null) {
            return null;
        }
        if (paramValues.length == 0) {
            return new Integer[0];
        }
        Integer[] values = new Integer[paramValues.length];
        for (int i = 0; i < paramValues.length; i++) {
            values[i] = Integer.valueOf(paramValues[i]);
        }
        return values;
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static Double getDoubleParam(ServletRequest request, String name, Double defaultNum) {
        String temp = request.getParameter(name);
        if (temp == null || "".equals(temp.trim())) {
            return defaultNum;
        }
        return Double.valueOf(temp);
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static Long getLongParam(ServletRequest request, String name, Long defaultNum) {
        String temp = request.getParameter(name);
        if (temp == null || "".equals(temp.trim())) {
            return defaultNum;
        }
        return Long.valueOf(temp);
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static Long[] getLongParams(ServletRequest request, String name) {
        String[] paramValues = request.getParameterValues(name);
        if (paramValues == null) {
            return null;
        }
        if (paramValues.length < 1) {
            return new Long[0];
        }
        Long[] values = new Long[paramValues.length];
        for (int i = 0; i < paramValues.length; i++) {
            values[i] = Long.valueOf(paramValues[i]);
        }

        return values;
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static String getAttr(ServletRequest request, String name) {
        return getAttr(request, name, "");
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static String getAttr(ServletRequest request, String name, String defaultStr) {
        String temp = (String) request.getAttribute(name);
        if (temp == null || "".equals(temp.trim())) {
            return defaultStr;
        }
        return temp;
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static Integer getIntAttr(ServletRequest request, String name) {
        return getIntAttr(request, name, null);
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static Integer getIntAttr(ServletRequest request, String name, Integer defaultNum) {
        Integer temp = (Integer) request.getAttribute(name);
        if (temp == null) {
            return defaultNum;
        }
        return temp;
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static Boolean getBooleanAttr(ServletRequest request, String name) {
        Boolean flag = (Boolean) request.getAttribute(name);
        if (flag == null) {
            return false;
        }
        return flag;
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static Long getLongAttr(ServletRequest request, String name) {
        return getLongAttr(request, name, null);
    }

    /**
     * 获取参数
     *
     * @param request
     * @param name
     * @return
     * @author 冯赵杨
     * @create 2018/8/31
     */
    public static Long getLongAttr(ServletRequest request, String name, Long defaultNum) {
        Long temp = (Long) request.getAttribute(name);
        if (temp == null) {
            return defaultNum;
        }
        return temp;
    }


}